﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.Business;
namespace ForkCarRelationManager
{
    public partial class AddRepireForm : Form
    {
        ClientBI clientBI = new ClientBI();
        RepireBI repireBI = new RepireBI();
        MaintenBI maintenBI = new MaintenBI();
        bool newclient = true;//默认新客户
        bool ifexist = true;//老客户
        int scompanyID = 0;
        DataTable clientTable;
        public AddRepireForm()
        {
            InitializeComponent();
        }
        private void AddRepireForm_Load(object sender, EventArgs e)
        {
            init();
        }
        private void init()
        {
            TBclient.DataSource = null ;
            //查找所有分公司作为数据源
                          
            DataTable companyTable = clientBI.initCompany();
            DataRow maintordr = companyTable.NewRow();
            maintordr[0] = "0";
            maintordr[1] = "";
            companyTable.Rows.InsertAt(maintordr, 0);
          //  TBCompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBCompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBCompany.DataSource = companyTable;
            this.TBCompany.DisplayMember = "ScompanyName";
            this.TBCompany.ValueMember = "ID";
        }
        private void buttonX1_Click(object sender, EventArgs e)
        {
            string ClientName = TBclient.Text.Trim();
            string ContactName = TBConName.Text.Trim();
            string ContactPhone = TBConphone.Text.Trim();//前3必要
            string ClientPlace = TBPlace.Text.Trim();
            string RepairContent = RepairContenttext.Text.Trim();
            string RequiredDate = TBrequiredate.Text.Trim();
            if (ClientName == "" || ContactName == "" || ContactPhone == ""  || RepairContent == "" || RequiredDate == ""||scompanyID==0)
            {
                MessageBox.Show("打星号的必填项不能为空");
            }
            else
            {

                if (newclient)
                {
                    int clientid = clientBI.addClientandgetid(ClientName, "", scompanyID.ToString(), ContactName, "", "", ContactPhone, "", ClientPlace, "");
                    if (clientid != 0)
                    {
                        if (repireBI.addRepire(ClientName,TBCompany.Text.Trim(), ContactName, ContactPhone, ClientPlace, clientid, RepairContent, RequiredDate, DateTime.Now))
                        {
                            MessageBox.Show("添加成功");
                            this.DialogResult = DialogResult.Yes;
                        }
                    }
                    else
                        MessageBox.Show("添加用户信息失败");

                }
                else
                {
                    if (maintenBI.SearchclientIDbyname(ClientName) != null)
                    {
                        int clientid = Convert.ToInt32( TBclient.SelectedValue.ToString());
                        if (repireBI.addRepire(ClientName,TBCompany.Text.Trim(),ContactName, ContactPhone, ClientPlace, clientid, RepairContent, RequiredDate, DateTime.Now))
                        {
                            MessageBox.Show("添加成功");
                            this.DialogResult = DialogResult.Yes;
                        }
                    }
                    else
                        MessageBox.Show("该客户不是老客户");
                }
            }
        }

        private void TBclient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TBclient.SelectedIndex != 0 && TBclient.SelectedValue!=null)
            {
                scompanyID = maintenBI.Getscompanyidbycid(TBclient.SelectedValue.ToString());
                DataTable clienttable=maintenBI.Getcontact(TBclient.SelectedValue.ToString());
                if (clienttable != null)
                {
                    TBConName.Text = clienttable.Rows[0][0].ToString();
                    TBConphone.Text = clienttable.Rows[0][1].ToString();
                }
                if (scompanyID != 0)
                {
                    TBCompany.Text = maintenBI.Getscompanynamebycid(scompanyID);
                }
            }
        }

        private void labelX6_Click(object sender, EventArgs e)
        {

        }

        private void labelX13_Click(object sender, EventArgs e)
        {

        }

        private void LabConphone_Click(object sender, EventArgs e)
        {

        }

        private void labelX5_Click(object sender, EventArgs e)
        {

        }

        private void labelX2_Click(object sender, EventArgs e)
        {

        }
        //新
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            TBConphone.Enabled = true;
            TBConName.Enabled = true;
            newclient = true;
            TBCompany.Enabled = true;
            TBclient.DataSource = null;
            DataTable companyTable = clientBI.initCompany();
            DataRow maintordr = companyTable.NewRow();
            maintordr[0] = "0";
            maintordr[1] = "";
            companyTable.Rows.InsertAt(maintordr, 0);
         // TBCompany.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBCompany.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBCompany.DataSource = companyTable;
            this.TBCompany.DisplayMember = "ScompanyName";
            this.TBCompany.ValueMember = "ID";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            TBConphone.Enabled = false;
            TBConName.Enabled = false;
            newclient = false;
            TBCompany.Enabled = false;
            //查找所有客户作为数据源
            clientTable = maintenBI.Getallclient();
            DataRow clientdr = clientTable.NewRow();
            clientdr[0] = "0";
            clientdr[1] = "";
            clientTable.Rows.InsertAt(clientdr, 0);
             TBclient.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充 
            TBclient.AutoCompleteSource = AutoCompleteSource.ListItems;
            this.TBclient.DataSource = clientTable;
            this.TBclient.DisplayMember = "ClientName";//客户所看到的
            this.TBclient.ValueMember = "ID";//标识*/
        }

        private void TBCompany_SelectionChangeCommitted(object sender, EventArgs e)
        {
            scompanyID = Convert.ToInt32(TBCompany.SelectedValue.ToString());
        }

     

      
    }
}
